import { AxiosRequestConfig } from 'axios';

import { RequestOptions } from '@/types/axios';
import { request } from '@/utils/request';

type anyObject = {
  [key: string]: any;
};

type configObject = {
  [method in 'list' | 'page' | 'create' | 'get' | 'update' | 'delete']?: {
    config?: AxiosRequestConfig;
    options?: RequestOptions;
  };
};

export default (url: string, config?: configObject) => {
  const list = (params?: anyObject) =>
    request.get({ url: `${url}/list`, params, ...config?.list.config }, config?.list.options);
  const page = (params?: anyObject) =>
    request.get({ url: `${url}/page`, params, ...config?.page.config }, config?.page.options);
  const create = (data?: anyObject) =>
    request.post({ url: `${url}/create`, data, ...config?.create.config }, config?.create.options);
  const get = (params?: anyObject) =>
    request.get({ url: `${url}/get`, params, ...config?.get.config }, config?.get.options);
  const update = (data?: anyObject) =>
    request.put({ url: `${url}/update`, data, ...config?.update.config }, config?.update.options);
  const Delete = (params?: anyObject) =>
    request.delete({ url: `${url}/delete?id=${params.id}`, params, ...config?.delete.config }, config?.delete.options);

  return {
    list,
    page,
    create,
    get,
    update,
    delete: Delete,
  };
};
